﻿using System;
using System.Collections.Generic;
using System.Text;
using DAL;
using System.Linq;

namespace BIZ
{
    public class ProductBIZ
    {
        public List<PRODUCT> GetAll()
        {
            return WebUtils.db.PRODUCTs.ToList();
        }

        public List<PRODUCT> GetByCategory(Guid categoryID)
        {
            var list = (from p in WebUtils.db.PRODUCTs where p.CategoryID.Equals(categoryID) select p);
            return list.ToList();
        }

        public List<PRODUCT> GetByCategoryForUser(Guid categoryID)
        {
            var list = (from p in WebUtils.db.PRODUCTs where p.CategoryID.Equals(categoryID) && p.IsLock == false select p);
            return list.ToList();
        }

        public PRODUCT GetByID(Guid productID)
        {
            var product = (from p in WebUtils.db.PRODUCTs where p.ProductID.Equals(productID) select p).FirstOrDefault();
            return product;
        }

        public PRODUCT GetByIDForUser(Guid productID)
        {
            var product = (from p in WebUtils.db.PRODUCTs where p.ProductID.Equals(productID) && p.IsLock==false select p).FirstOrDefault();
            return product;
        }

        public void Insert(PRODUCT product)
        {
            product.ProductID = Guid.NewGuid();
            product.CreateDate = DateTime.Now;

            WebUtils.db.PRODUCTs.InsertOnSubmit(product);
            WebUtils.db.SubmitChanges();

            UrlBIZ.GetAllUrl();
        }

        public void Edit(PRODUCT product)
        {
            var pro = (from p in WebUtils.db.PRODUCTs where p.ProductID.Equals(product.ProductID) select p).FirstOrDefault();
            var category = (from c in WebUtils.db.CATEGORies where c.CategoryID.Equals(product.CategoryID) select c).FirstOrDefault();

            pro.Name = product.Name;
            pro.Url = product.Url;
            pro.Price = product.Price;
            pro.Description = product.Description;
            pro.Image = product.Image;
            pro.CATEGORY = category;
            pro.InStock = product.InStock;
            
            WebUtils.db.SubmitChanges();
            UrlBIZ.GetAllUrl();
        }

        public void ChangeStatus(Guid id, bool IsLock)
        {
            PRODUCT p = WebUtils.db.PRODUCTs.Single(product => product.ProductID.Equals(id));
            p.IsLock = IsLock;

            WebUtils.db.SubmitChanges();
        }

        public List<PRODUCT> Search(string text)
        {
            var list = (from p in WebUtils.db.PRODUCTs where p.Name.Contains(text) && p.IsLock == false select p).ToList();

            return list;
        }
    }
}
